﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using SAPbouiCOM;
using System.Text.RegularExpressions;
using BoAddonBiz_FreightForwarder.UpLoad;
using SAPbobsCOM;
using System.Collections;

namespace BoAddonBiz_FreightForwarder.BusinessBase
{
    public abstract class B1Form_SalesOrder_Base : SAPBoAddon.B1Form
    {
        #region abstract variable
        protected abstract string AVA_OSOD { get; }
        protected abstract string AVA_SOD1 { get; }
        protected abstract string AVA_SOD2 { get; }
        protected abstract string AVA_SOD3 { get; }
        protected abstract string AVA_SOD4 { get; }
        protected abstract string AVA_SOD5 { get; }
        protected abstract string AVA_OQOT { get; }
        protected abstract string AVA_QOT3 { get; }
        protected abstract string AVA_QOT4 { get; }
        protected abstract string TargetObject { get; }
        protected abstract string BaseObject { get; }
        protected abstract string[] SumMapping { get; }
        #endregion

        #region global variable
        protected int CargoCurrentOffset = 0;//货物信息的当前行
        protected int CompanyCurrentOffset = 0;//公司信息的当前行
        protected int FileCurrentOffset = 0;//附件信息的当前行
        protected readonly string[] CargoInfoSumItem = new string[] { "cargo_6", "cargo_8", "cargo_10", "cargo_12", "cargo_40", "cargo_24" };
        #region 货物
        private Dictionary<string, string> _Cargo_ComboBoxUID_Field;
        /// <summary>
        /// 货物信息中ComboBox控件
        /// </summary>
        protected Dictionary<string, string> Cargo_ComboBoxUID_Field
        {
            get
            {
                if (_Cargo_ComboBoxUID_Field == null)
                {
                    _Cargo_ComboBoxUID_Field = new Dictionary<string, string>();
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(this.CurrentForm.GetAsXML());
                    XmlNodeList nodes = xml.ChildNodes[1].SelectNodes(String.Format("//item[@type='113']/specific/databind[@table='{0}']", AVA_SOD1));
                    foreach (XmlNode node in nodes)
                    {
                        string uid = node.ParentNode.ParentNode.Attributes["uid"].Value;
                        string alias = node.Attributes["alias"].Value;
                        _Cargo_ComboBoxUID_Field.Add(uid, alias);
                    }
                }
                return _Cargo_ComboBoxUID_Field;
            }
        }

        private Dictionary<string, string> _SalesOrder_ComboBoxUID_Field;
        /// <summary>
        /// 主表ComboBox控件
        /// </summary>
        protected Dictionary<string, string> SalesOrder_ComboBoxUID_Field
        {
            get
            {
                if (_SalesOrder_ComboBoxUID_Field == null)
                {
                    _SalesOrder_ComboBoxUID_Field = new Dictionary<string, string>();
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(this.CurrentForm.GetAsXML());
                    XmlNodeList nodes = xml.ChildNodes[1].SelectNodes(String.Format("//item[@type='113']/specific/databind[@table='{0}']", AVA_OSOD));
                    foreach (XmlNode node in nodes)
                    {
                        string uid = node.ParentNode.ParentNode.Attributes["uid"].Value;
                        string alias = node.Attributes["alias"].Value;
                        _SalesOrder_ComboBoxUID_Field.Add(uid, alias);
                    }
                }
                return _SalesOrder_ComboBoxUID_Field;
            }
        }
        #endregion
        #endregion

        #region constructor
        public B1Form_SalesOrder_Base()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_SalesOrder_Base_frm_ItemEvent);
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_SalesOrder_Base_frm_ChooseFormListEvent);
            this.frm_FormDataEvent += new SAPBoAddon.IB1FormEvents.B1FormDataEventEventHandler(B1Form_SalesOrder_Base_frm_FormDataEvent);
            this.frm_MenuEvent += new SAPBoAddon.IB1FormEvents.B1MenuEventEventHandler(B1Form_SalesOrder_Base_frm_MenuEvent);
        }

        #endregion

        #region override methods
        protected override bool BeforeSaveCheckFormData()
        {
            return Utils.CheckBusinessPartner(SBOApp, CurrentForm, AVA_OSOD, "U_CardName");
        }
        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
            switch (frmEditMode)
            {
                case SAPBoAddon.frmItemEditMode.fe_AddMode:
#if DEBUG
                    DateTime dt1;
                    DateTime dt2;
                    TimeSpan ts;
                    dt1 = DateTime.Now;
#endif
                    SAPbouiCOM.DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                    //加载操作员信息
                    string Operator = Utils.GetCurrentUser().Operator; ;
                    dbds.SetValue("U_Operator", dbds.Offset, Operator);
                    Utils.SelectShowEmpId(dbds, Operator);
                    dbds.SetValue("U_DocDate", dbds.Offset, DateTime.Now.ToString("yyyyMMdd"));
                    dbds.SetValue("UserSign", dbds.Offset, SBOCompany.UserSignature.ToString());
                    /*显示上传图片*/
                    this.Matrix_AutoAddLine();
                    //带出订单跟踪人名称
                    GetSlpName();
                    /*下拉选默认值*/
                    this.ComboBox_SelectDefault();
#if DEBUG
                    dt2 = DateTime.Now;
                    ts = dt2 - dt1;
                    SBOApp.StatusBar.SetText(string.Format("加载cargo默认值完成,耗时:[{0}]", ts.TotalMilliseconds.ToString()), BoMessageTime.bmt_Medium, BoStatusBarMessageType.smt_Success);
#endif
                    break;
            }
            Utils.ShowDBDSNum(CurrentForm, AVA_SOD1, 0);/*显示行号、总行数*/
            Utils.ShowDBDSNum(CurrentForm, AVA_SOD2, 0, "ComLine", "ComTal"); /*公司显示行号、总行数*/
            Utils.ShowDBDSNum(CurrentForm, AVA_SOD5, 0, "FileLine", "FileTal"); /*附件显示行号、总行数*/
        }
        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.AutoGetObjectNextNumber = true;

            #region 控件是否可编辑
            string[] ItemList;
            string[] itmBrowseModeDisable;
            string[] itmBrowseModeEnable;
            string[] itmFindModeEnable;

            string[] filter = new string[] { "btn_Send", "139", "1000003", "CaPrevious", "CargoNext", "CarSurvey", "FlPrevious", "FileNext", "FleSurvey" };
            string[] NewModeItms = new string[] { "3", "7", "35", "1000003", "194" };

            #region 添加状态
            FormSetting.FormModeSetting.itmNewModeEnable = Utils.GetFormItems(FormSetting.FormXmlDocument, true, NewModeItms);
            FormSetting.FormModeSetting.itmNewModeDisable = NewModeItms;
            #endregion

            FormSetting.FormModeSetting.StatusItemUID = "7";
            SAPBoAddon.BrowseModeSetting ModeSetting;


            ItemList = Utils.GetFormItems(FormSetting.FormXmlDocument, true, new string[] { "3", "194", "1000003" });
            itmBrowseModeEnable = new string[ItemList.Length];
            ItemList.CopyTo(itmBrowseModeEnable, 0);

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "P";
            ModeSetting.itmBrowseModeDisable = new string[] { "3", "35", "194", "1000003" };
            ModeSetting.itmBrowseModeEnable = itmBrowseModeEnable;


            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "A";
            ModeSetting.itmBrowseModeEnable = new string[] { "7" };
            itmBrowseModeDisable = new string[filter.Length + 1];
            filter.CopyTo(itmBrowseModeDisable, 0);
            itmBrowseModeDisable[itmBrowseModeDisable.Length - 1] = "7";
            ModeSetting.itmBrowseModeDisable = Utils.GetFormItems(FormSetting.FormXmlDocument, true, itmBrowseModeDisable);


            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "R";
            ModeSetting.itmBrowseModeDisable = Utils.GetFormItems(FormSetting.FormXmlDocument, true, new string[] { "btn_Send", "139",  "CaPrevious", "CargoNext", "CarSurvey", "FlPrevious", "FileNext", "FleSurvey" });
            //ModeSetting.itmBrowseModeEnable = itmBrowseModeEnable;

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "C";
            ModeSetting.itmBrowseModeDisable = itmBrowseModeDisable;
            //ModeSetting.itmBrowseModeEnable = itmBrowseModeEnable;

            ModeSetting = FormSetting.FormModeSetting.BrowseModeSettingList.Add();
            ModeSetting.Status = "D";
            ModeSetting.itmBrowseModeDisable = itmBrowseModeDisable;
            //ModeSetting.itmBrowseModeEnable = itmBrowseModeEnable;  

            //查找状态
            ItemList = Utils.GetFormItems(FormSetting.FormXmlDocument);
            itmFindModeEnable = new string[ItemList.Length + NewModeItms.Length];
            ItemList.CopyTo(itmFindModeEnable, 0);
            NewModeItms.CopyTo(itmFindModeEnable, ItemList.Length);
            FormSetting.FormModeSetting.itmFindModeEnable = itmFindModeEnable;
            #endregion
        }
        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            DateTime dt1 = DateTime.Now;
            this.CurrentForm.Freeze(true);
            /*选中第一个页签*/
            SAPbouiCOM.Folder oFolder = this.CurrentForm.Items.Item("tab_0").Specific;
            oFolder.Select();

            /*加载ComboBox可选值*/
            this.Combobox_Initialize();
            //得到当前用户
            Utils.LoadUser(this.CurrentForm, this.SBOCompany, "1000003");
            //审批人
            Utils.LoadUser(this.CurrentForm, this.SBOCompany, "txtApp");
            if (new Regex(@"AVA_\w{2}_Quote").Match(InitPar.FatherFormUID).Success)
            //if (InitPar.FatherFormUID.EndsWith("Quote"))
            {
                this.CurrentForm.DataSources.UserDataSources.Item("ParentUID").Value = InitPar.FatherFormUID;
                this.CurrentForm.Mode = BoFormMode.fm_ADD_MODE;
                CreateNewFromQuote(InitPar.FatherFormUID);


            }
            else if (new Regex(@"\b\d+\b").Match(InitPar.FatherFormUID).Success)
            {
                this.CurrentForm.Mode = SAPbouiCOM.BoFormMode.fm_FIND_MODE;
                SAPBoAddon.B1Assistant.Item.SetValue(this.CurrentForm, this.CurrentForm.DataBrowser.BrowseBy, InitPar.FatherFormUID);
                this.CurrentForm.Items.Item("1").Click(SAPbouiCOM.BoCellClickType.ct_Regular);
            }
            this.CurrentForm.Freeze(false);
#if DEBUG
            DateTime dt2 = DateTime.Now;
            TimeSpan ts = dt2 - dt1;
            string str_d = ts.TotalMilliseconds.ToString();
            SBOApp.StatusBar.SetText(string.Format("窗体加载完成,耗时:[{0}]", str_d), BoMessageTime.bmt_Medium, BoStatusBarMessageType.smt_Success);
#endif

        }
        #endregion

        #region events
        void B1Form_SalesOrder_Base_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            SAPbouiCOM.DBDataSource dbds = default(SAPbouiCOM.DBDataSource);
            SAPbouiCOM.Conditions cons;
            SAPbouiCOM.Condition con;
            SAPbouiCOM.ChooseFromList cfl;
            SAPbobsCOM.Recordset oRst = (SAPbobsCOM.Recordset)SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

            if (pVal.BeforeAction)
            {
                #region 事件之前过滤
                cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);
                switch (pVal.ChooseFromListUID)
                {
                    //客户编码 、客户名称、收货人编码
                    case "CFL_CardCode":
                    case "CFL_CardName":
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup13";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "CardType";
                        con.CondVal = "C";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_ShipperInfoCardCode":/*分单发货人*/
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup11";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_ConsigneeInfoCardCode": /*分单收货人*/
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup12";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_NotifyPartyCardCode":/*通知人*/
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup8";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    //项目
                    case "CFL_Project":
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "Active";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    //危险类别
                    case "CFL_Dgous":
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "Canceled";
                        con.CondVal = "N";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    ////发货人编码
                    //case "CFL_IssueSide":
                    //    cons = new SAPbouiCOM.Conditions();
                    //    con = cons.Add();
                    //    con.Alias = "CardType";
                    //    con.CondVal = "S";
                    //    con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                    //    cfl.SetConditions(cons);
                    //    break;
                    /*报价单choosefromlist*/
                    case "cfl_OQOT":
                        string CardCode = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD).GetValue("U_CardCode", 0).Trim();
                        if (string.IsNullOrEmpty(CardCode))
                        {
                            SBOApp.StatusBar.SetText("请先选择客户...", BoMessageTime.bmt_Short, BoStatusBarMessageType.smt_Warning);
                            BubbleEvent = false;
                            return;
                        }
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "U_CardCode";
                        con.CondVal = CardCode;
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;//根据客户编码过滤
                    //指定客服（员工）
                    case "CFL_Employee":
                        //根据组别选择相应客服(条件筛选)
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        Utils.SelectClientName(cfl);
                        //Utils.SelectClientName(cfl, dbds);
                        break;
                }
                #endregion
            }
            else
            {
                #region 选中赋值
                switch (pVal.ChooseFromListUID)
                {
                    //客户编码、客户名称
                    case "CFL_CardCode":
                    case "CFL_CardName":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_CardCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_CardName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        Utils.GetSalesPerson(dbds, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        //string sale = pVal.SelectedObjects.GetValue("U_Sale", 0).ToString();
                        //dbds.SetValue("U_BusinessMan", dbds.Offset, sale);
                        //if (!String.IsNullOrEmpty(sale))
                        //{
                        //    dbds.SetValue("U_BusinessMan", dbds.Offset, sale);
                        //    Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
                        //    rs.DoQuery(String.Format("select empID from OHEM where jobTitle = '{0}'", sale));
                        //    if (rs.RecordCount == 1)
                        //    {
                        //        dbds.SetValue("U_BnsManCode", dbds.Offset, rs.Fields.Item("empID").Value.ToString());
                        //    }
                        //}
                        //dbds.SetValue("U_CntctCode", dbds.Offset, pVal.SelectedObjects.GetValue("CntctPrsn", 0).ToString());
                        //dynamic Cntct = GetDefaultContractPerson(pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dynamic Cntct = Utils.GetDefaultContractPerson(this.SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        if (Cntct != null)
                        {
                            dbds.SetValue("U_CntctCode", dbds.Offset, Cntct.Name);
                            dbds.SetValue("U_CntctPhone", dbds.Offset, Cntct.Cellolar);
                            dbds.SetValue("U_CntctFax", dbds.Offset, Cntct.Fax);
                        }
                        else
                        {
                            dbds.SetValue("U_CntctCode", dbds.Offset, "");
                            dbds.SetValue("U_CntctPhone", dbds.Offset, "");
                            dbds.SetValue("U_CntctFax", dbds.Offset, "");
                        }
                        break;
                    //项目
                    case "CFL_Project":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_PrjCode", dbds.Offset, pVal.SelectedObjects.GetValue("PrjCode", 0).ToString());
                        dbds.SetValue("U_PrjName", dbds.Offset, pVal.SelectedObjects.GetValue("PrjName", 0).ToString());
                        break;
                    //危险类别
                    case "CFL_Dgous":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD1);
                        dbds.SetValue("U_DangerPro", dbds.Offset, pVal.SelectedObjects.GetValue("U_DCName", 0).ToString());
                        break;
                    ////发货人编码
                    //case "CFL_IssueSide":
                    //    dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD2);
                    //    dbds.SetValue("U_ConsignorCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    //    dbds.SetValue("U_ConsignorName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    //    dynamic CntctLine = Utils.GetDefaultContractPerson(SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    //    if (CntctLine != null)
                    //    {
                    //        dbds.SetValue("U_ConsignorContact", dbds.Offset, CntctLine.FullName);
                    //        dbds.SetValue("U_Consignorphone", dbds.Offset, CntctLine.Cellolar);
                    //        dbds.SetValue("U_ConsignorAddress", dbds.Offset, CntctLine.Address);
                    //        dbds.SetValue("U_Consignoremail", dbds.Offset, CntctLine.Email);
                    //    }
                    //    //omtx.SetLineData(pVal.Row);
                    //    break;
                    ////收货人编码
                    //case "CFL_CardCode2":
                    //    dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD2);
                    //    dbds.SetValue("U_ConsigneeNo", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    //    dbds.SetValue("U_ConsigneeName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    //    //dbds.SetValue("U_ConsignorContact", dbds.Offset, pVal.SelectedObjects.GetValue("CntctPrsn", 0).ToString());
                    //    dynamic CntcLine = Utils.GetDefaultContractPerson(SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                    //    if (CntcLine != null)
                    //    {
                    //        dbds.SetValue("U_DeliveryMan", dbds.Offset, CntcLine.FullName);
                    //        dbds.SetValue("U_Deliveryphone", dbds.Offset, CntcLine.Cellolar);
                    //        dbds.SetValue("U_DeliveryAddress", dbds.Offset, CntcLine.Address);
                    //        dbds.SetValue("U_Deliveryemail", dbds.Offset, CntcLine.Email);
                    //    }
                    //    break;
                    //起运港
                    case "CFL_PORT_S":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_StartPort", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortCode", 0).ToString());
                        dbds.SetValue("U_StartPortName", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortName", 0).ToString());
                        dbds.SetValue("U_StartPortEName", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortNameE", 0).ToString());
                        break;
                    //目的港
                    case "CFL_PORT_E":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_EndPort", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortCode", 0).ToString());
                        dbds.SetValue("U_EndPortName", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortName", 0).ToString());
                        dbds.SetValue("U_EndPortEName", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortNameE", 0).ToString());
                        break;
                    case "cfl_OQOT":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_BaseEntry", dbds.Offset, pVal.SelectedObjects.GetValue("DocEntry", 0).ToString().Trim());
                        dbds.SetValue("U_PriceSheetNo", dbds.Offset, pVal.SelectedObjects.GetValue("U_SerialNum", 0).ToString().Trim());
                        CopyFrom(pVal);
                        break;
                    //中转港
                    case "CFL_PORT_T":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_TrsferPort", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortCode", 0).ToString());
                        dbds.SetValue("U_TrsferPortName", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortName", 0).ToString());
                        dbds.SetValue("U_TrsferPortEName", dbds.Offset, pVal.SelectedObjects.GetValue("U_PortNameE", 0).ToString());
                        break;
                    //指定客服（员工）
                    case "CFL_Employee":
                        pVal.AutoFillValue = false;
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        string ServiceName = pVal.SelectedObjects.GetValue("jobTitle", 0).ToString();
                        if (string.IsNullOrEmpty(ServiceName))
                            ServiceName = pVal.SelectedObjects.GetValue("firstName", 0) + pVal.SelectedObjects.GetValue("lastName", 0);
                        dbds.SetValue("U_ServiceName", dbds.Offset, ServiceName);
                        if (CurrentForm.Mode == BoFormMode.fm_OK_MODE || CurrentForm.Mode == BoFormMode.fm_VIEW_MODE)
                        {
                            CurrentForm.Mode = BoFormMode.fm_UPDATE_MODE;
                        }
                        break;
                    //业务员
                    case "CFL_SalesPerson":
                        pVal.AutoFillValue = false;
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_BusinessMan", dbds.Offset, pVal.SelectedObjects.GetValue("SlpName", 0).ToString());
                        dbds.SetValue("U_BnsManCode", dbds.Offset, pVal.SelectedObjects.GetValue("SlpCode", 0).ToString());
                        if (CurrentForm.Mode == BoFormMode.fm_OK_MODE || CurrentForm.Mode == BoFormMode.fm_VIEW_MODE)
                        {
                            CurrentForm.Mode = BoFormMode.fm_UPDATE_MODE;
                        }
                        break;
                    case "CFL_Operator":
                        pVal.AutoFillValue = false;
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        string Operator = pVal.SelectedObjects.GetValue("jobTitle", 0).ToString();
                        if (string.IsNullOrEmpty(Operator))
                        {
                            Operator = pVal.SelectedObjects.GetValue("firstName", 0).ToString().Trim() + pVal.SelectedObjects.GetValue("lastName", 0).ToString().Trim();
                            SBOApp.SetStatusBarMessage("英文名称不存在！", BoMessageTime.bmt_Short, false);
                        }
                        dbds.SetValue("U_Operator", dbds.Offset, Operator);
                        Utils.SelectShowEmpId(dbds, Operator);
                        if (CurrentForm.Mode == BoFormMode.fm_OK_MODE || CurrentForm.Mode == BoFormMode.fm_VIEW_MODE)
                        {
                            CurrentForm.Mode = BoFormMode.fm_UPDATE_MODE;
                        }
                        break;
                    /*通知人*/
                    case "CFL_NotifyPartyCardCode":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_NotifyCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_NotifyParty", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardFName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardName", 0).ToString() : pVal.SelectedObjects.GetValue("CardFName", 0).ToString());
                        Utils.SelectAddressDetail(this.CurrentForm, "893", AVA_OSOD, "U_NotifyPartyInfo", pVal.SelectedObjects.GetValue("CardCode", 0).ToString(), true);
                        //dbds.SetValue("U_NotifyPartyInfo", dbds.Offset, pVal.SelectedObjects.GetValue("U_DeliveryContent", 0).ToString());
                        break;
                    /*分单收货人*/
                    case "CFL_ConsigneeInfoCardCode":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_consigneeInfo", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardFName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardName", 0).ToString() : pVal.SelectedObjects.GetValue("CardFName", 0).ToString());
                        dbds.SetValue("U_consigneeCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        Utils.SelectAddressDetail(this.CurrentForm, "892", AVA_OSOD, "U_ConsigneeDetInfo", pVal.SelectedObjects.GetValue("CardCode", 0).ToString(), true);
                        //dbds.SetValue("U_ConsigneeDetInfo", dbds.Offset, pVal.SelectedObjects.GetValue("U_DeliveryContent", 0).ToString());
                        Utils.CargoInfoToBpTable(this.CurrentForm, pVal, IssueOrReceipt.Receipt, AVA_SOD2);
                        break;
                    /*分单发货人*/
                    case "CFL_ShipperInfoCardCode":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_ShipperCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ShipperInfo", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardFName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardName", 0).ToString() : pVal.SelectedObjects.GetValue("CardFName", 0).ToString());
                        Utils.SelectAddressDetail(this.CurrentForm, "891", AVA_OSOD, "U_ShipperDetInfo", pVal.SelectedObjects.GetValue("CardCode", 0).ToString(), true);
                        //dbds.SetValue("U_ShipperDetInfo", dbds.Offset, pVal.SelectedObjects.GetValue("U_DeliveryContent", 0).ToString());
                        Utils.CargoInfoToBpTable(this.CurrentForm, pVal, IssueOrReceipt.Issue, AVA_SOD2);
                        break;
                    //跟踪人
                    case "CFL_SlpCode":
                        pVal.AutoFillValue = false;
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        string SlpName = pVal.SelectedObjects.GetValue("jobTitle", 0).ToString();
                        if (string.IsNullOrEmpty(SlpName))
                        {
                            SlpName = pVal.SelectedObjects.GetValue("firstName", 0).ToString().Trim() + pVal.SelectedObjects.GetValue("lastName", 0).ToString().Trim();
                            SBOApp.SetStatusBarMessage("英文名称不存在！", BoMessageTime.bmt_Short, false);
                        }
                        dbds.SetValue("U_SlpCode", dbds.Offset, SlpName);
                        if (CurrentForm.Mode == BoFormMode.fm_OK_MODE || CurrentForm.Mode == BoFormMode.fm_VIEW_MODE)
                        {
                            CurrentForm.Mode = BoFormMode.fm_UPDATE_MODE;
                        }
                        break;
                    case "CFL_ODCM":

                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD1);
                        dbds.SetValue("U_DangerPro", dbds.Offset, pVal.SelectedObjects.GetValue("U_DCName", 0).ToString());
                        break;
                    //目的港代理
                    case "CFL_EPAgencyNameCardCode":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_EPAgencyCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_EPAgencyName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        break;
                    //启运港代理
                    case "CFL_SPAgencyInfoCardCode":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_SPAgencyCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_SPAgencyName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        break;
                    //中转港代理
                    case "CFL_TPAgencyInfoCardCode":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_TPAgencyCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_TPAgencyName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        break;
                    //承运人
                    case "CFL_CarrierNameCardCode":
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                        dbds.SetValue("U_CarrierCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_CarrierName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        break;
                    case "CFL_ExpressCmp"://快递公司                       
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD5);
                        dbds.SetValue("U_ExpressCmp", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        break;
                    case "CFL_CardCode_Consignor"://公司信息收货人
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD2);
                        dbds.SetValue("U_ConsignorCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsignorName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardFName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardName", 0).ToString() : pVal.SelectedObjects.GetValue("CardFName", 0).ToString());
                        //dbds.SetValue("U_ConsignorAddress", dbds.Offset, pVal.SelectedObjects.GetValue("U_DeliveryContent", 0).ToString());
                        dynamic ConsignorContactInfo = Utils.GetDefaultContractPerson(SAPBoAddon.B1Addon.B1Addon.SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsignorContact", dbds.Offset, ConsignorContactInfo.FullName);
                        dbds.SetValue("U_Consignorphone", dbds.Offset, ConsignorContactInfo.Cellolar);
                        dbds.SetValue("U_ConsignorAddress", dbds.Offset, ConsignorContactInfo.Address);
                        dbds.SetValue("U_Consignoremail", dbds.Offset, ConsignorContactInfo.Email);
                        break;
                    case "CFL_CardCode_Consignee"://公司信息发货人
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD2);
                        dbds.SetValue("U_ConsigneeNo", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsigneeName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardFName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardName", 0).ToString() : pVal.SelectedObjects.GetValue("CardFName", 0).ToString());
                        dynamic ConsigneeContactInfo = Utils.GetDefaultContractPerson(SAPBoAddon.B1Addon.B1Addon.SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_DeliveryMan", dbds.Offset, ConsigneeContactInfo.FullName);
                        dbds.SetValue("U_Deliveryphone", dbds.Offset, ConsigneeContactInfo.Cellolar);
                        dbds.SetValue("U_DeliveryAddress", dbds.Offset, ConsigneeContactInfo.Address);
                        dbds.SetValue("U_Deliveryemail", dbds.Offset, ConsigneeContactInfo.Email);
                        break;
                }
                #endregion
            }

        }
        void B1Form_SalesOrder_Base_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            try
            {
                if (pVal.BeforeAction)
                {

                }
                else
                {
                    #region switch panel
                    if (pVal.EventType == BoEventTypes.et_ITEM_PRESSED && pVal.ItemUID.ToLower().StartsWith("tab_"))
                    {
                        switch (pVal.ItemUID)
                        {
                            case "tab_0":
                                this.CurrentForm.PaneLevel = 1;
                                break;
                            case "tab_1":
                                this.CurrentForm.PaneLevel = 2;
                                break;
                            case "tab_2":
                                this.CurrentForm.PaneLevel = 3;
                                break;
                            case "tab_3":
                                this.CurrentForm.PaneLevel = 4;
                                break;
                            case "tab_4":
                                this.CurrentForm.PaneLevel = 5;
                                break;
                            case "tab_5":
                                this.CurrentForm.PaneLevel = 6;
                                #region 是否存在附件
                                Common_UpLoad Uploading = new Common_UpLoad();
                                Uploading.DownButtonVisable(CurrentForm, AVA_SOD5, FileCurrentOffset);
                                #endregion

                                break;
                        }
                        #region MatrixSetting
                        //if (this.CurrentForm.PaneLevel == 3)
                        //{
                        //    this.CurrentForm.Settings.MatrixUID = "mtx_3";
                        //}

                        #endregion
                    }
                    #endregion
                    if (pVal.ItemUID == "139" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        if (this.CurrentForm.Mode == BoFormMode.fm_OK_MODE)
                        {
                            if (this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD).GetValue("U_Status", 0).Trim() == "R")
                            {
                                ShowSalesOrderListForm(CurrentForm);
                            }
                            else
                            {
                                SBOApp.StatusBar.SetText(String.Format("只有在批准状态下才能创建委托单"), SAPbouiCOM.BoMessageTime.bmt_Medium, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                            }
                        }
                        else
                        {
                            SBOApp.StatusBar.SetText(String.Format("请先点击“更新”或“添加”，保存当前单据。"), SAPbouiCOM.BoMessageTime.bmt_Medium, SAPbouiCOM.BoStatusBarMessageType.smt_Warning);
                        }
                    }
                    if (pVal.ItemUID == "215" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        //Utils.OpenUserDeFinedObjectForm(this.CurrentForm, pVal.ItemUID, "AVA_FF_Quote");
                        Utils.OpenUserDeFinedObjectForm(this.CurrentForm, pVal.ItemUID, BaseObject, this.ShowForm);
                    }
                    #region Linq+Lambda的总体积计算
                    if (pVal.EventType == BoEventTypes.et_COMBO_SELECT && new string[] { "cargo_32", "cargo_34" }.Contains<string>(pVal.ItemUID))
                    {
                        Utils.CalculateVolumeAndQuantity(SBOApp, SBOCompany, CurrentForm, AVA_SOD1, CargoCurrentOffset);
                        Utils.MaterialSum(this.CurrentForm, AVA_OSOD, AVA_SOD1, SumMapping);
                    }
                    if (pVal.ItemUID == "cargo_24" && pVal.EventType == BoEventTypes.et_VALIDATE)
                    {
                        Utils.CalculateVolumeAndQuantity(SBOApp, SBOCompany, CurrentForm, AVA_SOD1, CargoCurrentOffset);
                        Utils.MaterialSum(this.CurrentForm, AVA_OSOD, AVA_SOD1, SumMapping);
                    }
                    if (pVal.ItemUID == "cargo_8" && pVal.ItemChanged)
                    {
                        DBDataSource dbds1 = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD1);
                        dbds1.SetValue("U_BillingWeight", dbds1.Offset, SAPBoAddon.B1Assistant.Item.GetValue(this.CurrentForm, pVal.ItemUID).ToString());
                    }
                    #endregion
                    if (pVal.ItemUID == "cargo_42" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_COMBO_SELECT && pVal.ItemChanged)
                    {
                        DBDataSource dbds1 = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD1);
                        if (dbds1.Offset == 0)
                        {
                            DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
                            dbds.SetValue("U_CargoValueUnit", 0, dbds1.GetValue("U_Currency", 0));
                        }
                    }
                    #region  公司信息
                    //上一条
                    if (pVal.ItemUID == "CoPrevious" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        CompanyCurrentOffset = Utils.Previous(SBOApp, pVal.FormUID, AVA_SOD2);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD2, CompanyCurrentOffset, "ComLine", "ComTal"); /*公司显示行号、总行数*/
                    }
                    //下一条
                    if (pVal.ItemUID == "ComNext" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        CompanyCurrentOffset = Utils.Next(SBOApp, pVal.FormUID, AVA_SOD2);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD2, CompanyCurrentOffset, "ComLine", "ComTal"); /*公司显示行号、总行数*/
                    }
                    //添加
                    if (pVal.ItemUID == "ComAdd" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        CompanyCurrentOffset = Utils.Add(SBOApp, pVal.FormUID, AVA_SOD2);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD2, CompanyCurrentOffset, "ComLine", "ComTal"); /*公司显示行号、总行数*/
                    }
                    //删除
                    if (pVal.ItemUID == "ComDel" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        CompanyCurrentOffset = Utils.Delete(SBOApp, pVal.FormUID, AVA_SOD2, CompanyCurrentOffset);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD2, CompanyCurrentOffset, "ComLine", "ComTal"); /*公司显示行号、总行数*/
                    }
                    //浏览所有
                    if (pVal.ItemUID == "ComSurvey" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        Utils.Survey(SBOApp, SBOCompany, pVal.FormUID, AVA_SOD2);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD2, CompanyCurrentOffset, "ComLine", "ComTal"); /*公司显示行号、总行数*/
                    }
                    #endregion
                    #region  货物信息
                    //上一条
                    if (pVal.ItemUID == "CaPrevious" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        CargoCurrentOffset = Utils.Previous(SBOApp, pVal.FormUID, AVA_SOD1);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD1, CargoCurrentOffset); /*显示行号、总行数*/
                    }
                    //下一条
                    if (pVal.ItemUID == "CargoNext" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        CargoCurrentOffset = Utils.Next(SBOApp, pVal.FormUID, AVA_SOD1);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD1, CargoCurrentOffset);/*显示行号、总行数*/
                    }
                    //添加
                    if (pVal.ItemUID == "CargoAdd" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        CargoCurrentOffset = Utils.Add(SBOApp, pVal.FormUID, AVA_SOD1);
                        //下拉列表框的默认值
                        Utils.ComboBox_Cargo_SelectDefault(this.CurrentForm, Cargo_ComboBoxUID_Field, AVA_SOD1);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD1, CargoCurrentOffset); /*显示行号、总行数*/
                    }
                    //删除
                    if (pVal.ItemUID == "CargoDel" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        CargoCurrentOffset = Utils.Delete(SBOApp, pVal.FormUID, AVA_SOD1, CargoCurrentOffset);
                        Utils.MaterialSum(this.CurrentForm, AVA_OSOD, AVA_SOD1, SumMapping);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD1, CargoCurrentOffset);/*显示行号、总行数*/
                    }
                    //货物信息相关Item-计算货物相关的总计
                    if (CargoInfoSumItem.Contains(pVal.ItemUID) && pVal.EventType == BoEventTypes.et_VALIDATE && pVal.ItemChanged)
                    {
                        Utils.MaterialSum(this.CurrentForm, AVA_OSOD, AVA_SOD1, SumMapping);
                    }
                    //浏览所有
                    if (pVal.ItemUID == "CarSurvey" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        Utils.Survey(SBOApp, SBOCompany, pVal.FormUID, AVA_SOD1);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD1, CargoCurrentOffset);/*显示行号、总行数*/
                    }
                    #endregion
                    #region  附件信息
                    //上一条
                    if (pVal.ItemUID == "FlPrevious" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        FileCurrentOffset = Utils.Previous(SBOApp, pVal.FormUID, AVA_SOD5);
                        Common_UpLoad Uploading = new Common_UpLoad();
                        Uploading.DownButtonVisable(CurrentForm, AVA_SOD5, FileCurrentOffset);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD5, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                    }
                    //下一条
                    if (pVal.ItemUID == "FileNext" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        FileCurrentOffset = Utils.Next(SBOApp, pVal.FormUID, AVA_SOD5);
                        Common_UpLoad Uploading = new Common_UpLoad();
                        Uploading.DownButtonVisable(CurrentForm, AVA_SOD5, FileCurrentOffset);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD5, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                    }
                    //添加
                    if (pVal.ItemUID == "FileAdd" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        FileCurrentOffset = Utils.Add(SBOApp, pVal.FormUID, AVA_SOD5);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD5, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                    }
                    //删除
                    if (pVal.ItemUID == "FileDel" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        FileCurrentOffset = Utils.Delete(SBOApp, pVal.FormUID, AVA_SOD5, FileCurrentOffset);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD5, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                    }
                    //浏览所有
                    if (pVal.ItemUID == "FleSurvey" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        Utils.Survey(SBOApp, SBOCompany, pVal.FormUID, AVA_SOD5);
                        Utils.ShowDBDSNum(CurrentForm, AVA_SOD5, FileCurrentOffset, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                    }
                    //选择文件路径
                    if (pVal.ItemUID == "Upload" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        Common_UpLoad upload = new Common_UpLoad();
                        upload.SetAffixInfo(CurrentForm, AVA_SOD5, FileCurrentOffset);
                    }
                    //下载上传的文件
                    if (pVal.ItemUID == "Down" && pVal.EventType == BoEventTypes.et_CLICK)
                    {
                        Common_UpLoad Uploading = new Common_UpLoad();
                        Uploading.DownFile(CurrentForm, AVA_SOD5, FileCurrentOffset);
                    }
                    #endregion
                    if (pVal.ItemUID == "btn_CFL8" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        string cardcode = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD).GetValue("U_ShipperCode", 0).Trim();
                        Utils.SelectAddressDetail(this.CurrentForm, "891", AVA_OSOD, "U_ShipperDetInfo", cardcode);
                    }
                    if (pVal.ItemUID == "btn_CFL9" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        string cardcode = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD).GetValue("U_consigneeCode", 0).Trim();
                        Utils.SelectAddressDetail(this.CurrentForm, "892", AVA_OSOD, "U_ConsigneeDetInfo", cardcode);
                    }
                    if (pVal.ItemUID == "btn_CFL10" && pVal.EventType == BoEventTypes.et_ITEM_PRESSED)
                    {
                        string cardcode = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD).GetValue("U_NotifyCode", 0).Trim();
                        Utils.SelectAddressDetail(this.CurrentForm, "893", AVA_OSOD, "U_NotifyPartyInfo", cardcode);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        void B1Form_SalesOrder_Base_frm_FormDataEvent(SAPBoAddon.B1AddonBase.B1BusinessObjectInfo pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {

            }
            else
            {
                #region 附件上传
                /*上传附件*/
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_ADD || pVal.EventType == BoEventTypes.et_FORM_DATA_UPDATE)
                {
                    try
                    {
                        Common_UpLoad oUpLoad = new Common_UpLoad();
                        oUpLoad.Uploading(this.CurrentForm, AVA_SOD5);
                        oUpLoad.DownButtonVisable(CurrentForm, AVA_SOD5, FileCurrentOffset, false);
                    }
                    catch (Exception ex)
                    {
                        SBOApp.SetStatusBarMessage("更新失败！错误信息：" + ex.Message, BoMessageTime.bmt_Short, false);
                        BubbleEvent = false;
                        CurrentForm.Mode = BoFormMode.fm_UPDATE_MODE;
                    }
                }
                #endregion
                if (pVal.EventType == BoEventTypes.et_FORM_DATA_LOAD)
                {
                    Common_UpLoad upload = new Common_UpLoad();
                    upload.DownButtonVisable(CurrentForm, AVA_SOD5, FileCurrentOffset);

                    Utils.ShowDBDSNum(CurrentForm, AVA_SOD1, 0);/*货物显示行号、总行数*/
                    Utils.ShowDBDSNum(CurrentForm, AVA_SOD2, 0, "ComLine", "ComTal"); /*公司显示行号、总行数*/
                    Utils.ShowDBDSNum(CurrentForm, AVA_SOD5, 0, "FileLine", "FileTal"); /*附件显示行号、总行数*/
                    #region 设置状态控件是否可编辑  由BeforAction里移动来的  by WangPeng 2012年8月29日 10:25:32 Vincent.yin
                    #endregion
                }

            }
        }
        void B1Form_SalesOrder_Base_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            {
                /*添加菜单*/
                if (pVal.MenuUID == "1292")
                {
                }
            }
            else
            {
                if (pVal.MenuUID == "1287")//复制
                {
                    //if (this.FormSetting.FormModeSetting.itmNewModeEnable == null)
                    //{
                    //    SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, this.FormSetting.FormModeSetting.itmBrowseModeEnable, true);
                    //}
                    //else
                    //{
                    //    SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, this.FormSetting.FormModeSetting.itmNewModeEnable, true);
                    //}
                    //SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, this.FormSetting.FormModeSetting.itmNewModeDisable, false);
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD).SetValue("U_PriceSheetNo", 0, "");
                    //附件不复制
                    this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD5).Clear();
                }
            }
        }
        #endregion

        #region private methods
        #region 从报价复制
        /// <summary>
        /// “复制从”
        /// </summary>
        private void CopyFrom(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal)
        {
            string oqot_entry = pVal.SelectedObjects.GetValue("DocEntry", 0).ToString();
            DBDataSource dbds_osod = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
            DBDataSource dbds_sod1 = this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD1);
            DBDataSource dbds_oqot = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OQOT);
            DBDataSource dbds_qot3 = this.CurrentForm.DataSources.DBDataSources.Item(AVA_QOT3);
            SAPbouiCOM.Conditions conditions = new Conditions();
            SAPbouiCOM.Condition condition = conditions.Add();
            condition.Alias = "DocEntry";
            condition.CondVal = oqot_entry;
            condition.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
            dbds_oqot.Query(conditions);
            dbds_qot3.Query(conditions);

            string sod_entry = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD).GetValue("DocEntry", 0).Trim();

            Dictionary<string, Object> osod_initial = new Dictionary<string, Object>();
            osod_initial.Add("DocEntry", sod_entry);
            osod_initial.Add("DocNum", sod_entry);
            osod_initial.Add("Period", 9);
            osod_initial.Add("Instance", 0);
            osod_initial.Add("Series", 64);
            osod_initial.Add("Handwrtten", "N");
            osod_initial.Add("Canceled", "N");
            osod_initial.Add("Object", this.CurrentForm.TypeEx);
            osod_initial.Add("LogInst", "");
            osod_initial.Add("UserSign", this.SBOCompany.UserSignature);
            osod_initial.Add("Transfered", "N");
            osod_initial.Add("Status", "O");
            osod_initial.Add("CreateDate", DateTime.UtcNow.ToString("yyyyMMdd"));
            osod_initial.Add("CreateTime", Convert.ToInt32(DateTime.UtcNow.ToString("HHmm")));
            osod_initial.Add("DataSource", "I");

            List<string> filter = new List<string>();//不复制的字段
            filter.Add("U_Status");
            filter.Add("U_SerialNum");
            Utils.CopyDBDS(dbds_oqot, dbds_osod, osod_initial, filter, null, true);

            Dictionary<string, Object> sod1_initial = new Dictionary<string, Object>();
            sod1_initial.Add("DocEntry", sod_entry);
            sod1_initial.Add("Object", this.CurrentForm.TypeEx);
            //sod1_initial.Add("U_ConNumber", pVal.SelectedObjects.GetValue("U_ContractNo", 0).ToString());
            Utils.CopyDBDS(dbds_qot3, dbds_sod1, sod1_initial);
            InitialUnit();

            Utils.MaterialSum(this.CurrentForm, AVA_OSOD, AVA_SOD1, SumMapping);//--统计总计字段 Add by Webb.Wen at 2012-03-09 10:24:20

            SerialNumber.GetInstance().GetSerialNumberByProc(this.CurrentForm);
        }
        private void InitialUnit()
        {
            string DefaultVal = string.Empty;
            string DefaultVal_WeightUnit = string.Empty;
            SAPbouiCOM.DBDataSource dbds_osod = CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);//AVA_OSOD
            foreach (var item in Cargo_ComboBoxUID_Field)
            {
                switch (item.Value)
                {
                    case "U_QuantityUnit":
                        DefaultVal = Utils.ComboBox_SelectDefault("Code", "Name", "@AVA_FF_OPRI", string.Empty, "U_Type = 'Number'");
                        dbds_osod.SetValue(item.Value, dbds_osod.Offset, DefaultVal);
                        break;
                    case "U_GrossWeightUnit":
                    case "U_BillingWeightUnit":
                        if (string.IsNullOrEmpty(DefaultVal_WeightUnit))
                        {
                            DefaultVal_WeightUnit = Utils.ComboBox_SelectDefault("Code", "Name", "@AVA_FF_OPRI", string.Empty, "U_Type = 'Weight'");
                        }
                        DefaultVal = DefaultVal_WeightUnit;
                        dbds_osod.SetValue(item.Value, dbds_osod.Offset, DefaultVal);
                        break;
                    case "U_VolumeUnit":
                        DefaultVal = Utils.ComboBox_SelectDefault("Code", "Name", "@AVA_FF_OPRI", string.Empty, "U_Type = 'Volume'");
                        dbds_osod.SetValue(item.Value, dbds_osod.Offset, DefaultVal);
                        break;
                    case "U_CargoValueUnit":
                        DefaultVal = Utils.ComboBox_SelectDefault("DocCurrCod", "CurrName", "OCRN");
                        dbds_osod.SetValue(item.Value, dbds_osod.Offset, DefaultVal);
                        break;
                }
                DefaultVal = string.Empty;
            }
        }
        private void CreateNewFromQuote(string fatherFormUID)
        {
            Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            string onnm = String.Format("select AutoKey from ONNM WITH(UPDLOCK) where ObjectCode = '{0}'", this.CurrentForm.TypeEx);
            rs.DoQuery(onnm);
            SAPbouiCOM.DBDataSource dbds_OSOD = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
            dbds_OSOD.SetValue("DocEntry", 0, rs.Fields.Item("AutoKey").Value.ToString());

            Form fatherForm = SBOApp.Forms.Item(fatherFormUID);
            List<string> filter = new List<string>();
            filter.Add("U_Status");
            filter.Add("U_SerialNum");
            Dictionary<string, string> mapping = new Dictionary<string, string>();
            mapping.Add("U_BaseEntry", "DocEntry");
            mapping.Add("U_PriceSheetNo", "U_SerialNum");
            mapping.Add("U_BusinessMan", "U_SlpCode");
            //mapping.Add("U_BaseEntry", "DocEntry");
            Utils.CopyDBDS(fatherForm.DataSources.DBDataSources.Item(AVA_OQOT), dbds_OSOD, null, filter, mapping, true);

            Dictionary<string, string> mapping1 = new Dictionary<string, string>();

            Utils.CopyDBDS(fatherForm.DataSources.DBDataSources.Item(AVA_QOT3), this.CurrentForm.DataSources.DBDataSources.Item(AVA_SOD1), null, null, mapping1, false);
            InitialUnit();
            Utils.MaterialSum(this.CurrentForm, AVA_OSOD, AVA_SOD1, SumMapping);//--统计总计字段 Add by Webb.Wen at 2012-03-09 10:24:20
            dbds_OSOD.SetValue("U_CargoValueUnit", dbds_OSOD.Offset, fatherForm.DataSources.DBDataSources.Item(AVA_QOT3).GetValue("U_Currency", 0).Trim());/*生成订单 带'货值单位'  added by WangPeng 2012-3-26*/
            dbds_OSOD.SetValue("U_TransportType", 0, fatherForm.DataSources.DBDataSources.Item(AVA_QOT4).GetValue("U_TransportMode", 0).Trim());
            //基于上游单据生成时，新单据总是所有控件均可以编辑，所以强制加上了这句话（by Webb.Wen at 2012-04-12 13:57:13）
            /*
             * 报价港口的“起运港”“中转港”“目的港”复制到订单的“操作要求”的“起运港”“中转港”“目的港”（经高旭2012-05-08 10:07:57跟璐璐询问，又都不用带了）
             */
            //DBDataSource dbds_QOT4 = fatherForm.DataSources.DBDataSources.Item(AVA_QOT4);

            //if (dbds_QOT4.Size > 0)
            //{
            //    dbds_OSOD.SetValue("U_StartPort", 0, dbds_QOT4.GetValue("U_StartPortCode", 0).Trim());
            //    dbds_OSOD.SetValue("U_StartPortEName", 0, dbds_QOT4.GetValue("U_StartPort", 0).Trim());
            //    dbds_OSOD.SetValue("U_TrsferPort", 0, dbds_QOT4.GetValue("U_TranshipPortCode", 0).Trim());
            //    dbds_OSOD.SetValue("U_TrsferPortEName", 0, dbds_QOT4.GetValue("U_TranshipPort", 0).Trim());
            //    dbds_OSOD.SetValue("U_EndPort", 0, dbds_QOT4.GetValue("U_EndPortCode", 0).Trim());
            //    dbds_OSOD.SetValue("U_EndPortEName", 0, dbds_QOT4.GetValue("U_EndPort", 0).Trim());
            //}



            SAPBoAddon.B1Assistant.Item.SetEnabled(this.CurrentForm, FormSetting.FormModeSetting.itmNewModeDisable, false);

            SerialNumber.GetInstance().GetSerialNumberByProc(this.CurrentForm);
        }
        #endregion
        private void ShowSalesOrderListForm(SAPbouiCOM.Form oForm)
        {
            try
            {
                string config = "选择:1;:0;订单号:1;客户编码:1;客户名称:1";//"标题:显示"
                //this.ShowForm(B1FormTypes.ado_AVA_FF_SalesOrderList, this.CurrentForm.UniqueID);
                SAPbouiCOM.Form salesOrderListForm = SAPBoAddon.B1Assistant.Form.LoadToSBOApplciation(System.Windows.Forms.Application.StartupPath + @"\Form\AVA_FF_SalesOrderListForm.srf", "", true);
                string SODEntry = oForm.DataSources.DBDataSources.Item(AVA_OSOD).GetValue("DocEntry", 0);
                salesOrderListForm.Freeze(true);
                salesOrderListForm.DataSources.UserDataSources.Item("SODEntry").Value = SODEntry;
                salesOrderListForm.DataSources.UserDataSources.Item("Target").Value = TargetObject;
                salesOrderListForm.DataSources.UserDataSources.Item("fatherUID").Value = this.CurrentForm.UniqueID;
                SAPbouiCOM.Grid grid = salesOrderListForm.Items.Item("Grid").Specific;
                salesOrderListForm.Items.Item("Grid").Enabled = true;
                SAPbouiCOM.DataTable dt = salesOrderListForm.DataSources.DataTables.Add("dt");
                string select = String.Format("EXEC [AVA_SP_FilterSalesOrder] '{0}','{1}'", SODEntry, AVA_OSOD);
                dt.ExecuteQuery(select);
                grid.DataTable = dt;
                string[] configs = config.Split(';');
                int eof = (configs.Length <= dt.Columns.Count) ? configs.Length : dt.Columns.Count;

                for (int i = 0; i < eof; i++)
                {
                    string[] items = configs[i].Split(':');
                    grid.Columns.Item(i).TitleObject.Caption = items[0];
                    if (i == 0)
                    {
                        grid.Columns.Item(i).Type = SAPbouiCOM.BoGridColumnType.gct_CheckBox;//只有第一行可以编辑
                        grid.Columns.Item(i).Editable = true;
                    }
                    else
                    {
                        grid.Columns.Item(i).Editable = false;
                    }
                    grid.Columns.Item(i).Visible = items[1] == "1" ? true : false;
                }
                if (eof < grid.Columns.Count)
                {
                    for (int i = eof; i < grid.Columns.Count; i++)
                    {
                        grid.Columns.Item(i).Visible = false;
                    }
                }
                grid.AutoResizeColumns();
                salesOrderListForm.Freeze(false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 带出订单跟踪人名称
        /// </summary>
        private void GetSlpName()
        {
            SAPbouiCOM.DBDataSource dbds = default(SAPbouiCOM.DBDataSource);
            dbds = this.CurrentForm.DataSources.DBDataSources.Item(AVA_OSOD);
            //得到默认跟踪人的姓名
            Recordset ordSet = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            string SlpNameSelect = String.Format("select Top 1 jobTitle from OHEM where userId ={0} ", this.SBOCompany.UserSignature.ToString().Trim());
            ordSet.DoQuery(SlpNameSelect);
            if (ordSet.RecordCount > 0)
            {
                string SlpName = ordSet.Fields.Item("jobTitle").Value.ToString();
                dbds.SetValue("U_SlpCode", dbds.Offset, SlpName.ToString().Trim());
            }
        }
        /// <summary>
        /// 初始化下拉列表
        /// </summary>
        /// <author>WangPeng</author>
        private void Combobox_Initialize()
        {
            SAPbobsCOM.Recordset oRst = SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            Utils.ComboBox_Cargo_Initial(oRst, this.CurrentForm, Cargo_ComboBoxUID_Field);
            Utils.ComboBox_Main_Initial(oRst, this.CurrentForm, SalesOrder_ComboBoxUID_Field);
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("324"), "Code", "Name", "@AVA_FF_DTYP", "CAST(Code AS INT)");//文件类型 added by WangPeng 2012年12月13日 14:44:12
        }
        /// <summary>
        /// ComboBox选默认值
        /// </summary>
        /// <author>WangPeng</author>
        private void ComboBox_SelectDefault()
        {
            string DefaultVal = string.Empty;
            this.CurrentForm.Freeze(true);
            try
            {
                Utils.ComboBox_Main_SelectDefault(this.CurrentForm, AVA_OSOD, SalesOrder_ComboBoxUID_Field);
                Utils.ComboBox_Cargo_SelectDefault(this.CurrentForm, Cargo_ComboBoxUID_Field, AVA_SOD1);

                this.CurrentForm.Refresh();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CurrentForm.Freeze(false);
            }
        }
        #endregion
    }
}
